package com.gzy.mylog.modules.log;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.gzy.mylog.BaseActivity;
import com.gzy.mylog.R;
import com.gzy.mylog.events.AddLogEvent;
import com.gzy.mylog.events.DeleteLogEvent;
import com.gzy.mylog.events.UpdateLogEvent;
import com.gzy.mylog.manager.AuthManager;
import com.gzy.mylog.model.Log;
import com.gzy.mylog.model.Result;
import com.gzy.mylog.net.LogApi;
import com.gzy.mylog.net.core.XXModelCallback;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;

public class UpdateLogActivity extends BaseActivity {

    Long id;
    String content;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_log);

        id = getIntent().getLongExtra("id",0);
        content = getIntent().getStringExtra("content");

        findViewById(R.id.rl_update).setOnClickListener(this::submit);
        findViewById(R.id.rl_delete).setOnClickListener(this::delete);

        editText = findViewById(R.id.editText);

        editText.setText(content);
    }
    void delete(View view){
        LogApi.deleteLog(AuthManager.loadToken(this),id, new XXModelCallback<Result<String>>(new TypeReference<Result<String>>(){}) {
            @Override
            public void onResponseData(Call call, Result<String> model) {
                Toast.makeText(UpdateLogActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new DeleteLogEvent());
                finish();
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(UpdateLogActivity.this, "删除失败：" + message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    void submit(View view) {
        String content = editText.getText().toString().trim();
        if (TextUtils.isEmpty(content)){
            Toast.makeText(this, "请输入日志内容", Toast.LENGTH_SHORT).show();
            return;
        }

        LogApi.updateLog(AuthManager.loadToken(this),id,content, new XXModelCallback<Result<Log>>(new TypeReference<Result<Log>>(){}) {
            @Override
            public void onResponseData(Call call, Result<Log> model) {
                Toast.makeText(UpdateLogActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new UpdateLogEvent(model.getData()));
                finish();
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(UpdateLogActivity.this, "修改失败：" + message, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
